import { upload, useRequest } from "../../../utils/request";
import map from "../../../utils/map";

const WeValidator = require('../../../package/we-validator.min')

Component({
    properties: {},
    data: {
        maxLength: 100,
        textLength: 0,
        fileList: [],
        videoList: []
    },
    methods: {
        onLoad(options) {
            console.log(options)
            if (Number(options.type) === 1) {
                this.request = new useRequest('api/travelpost', {
                    hold: true
                }, this)
            } else {
                this.request = new useRequest('api/publicinfo', {
                    hold: true
                }, this)
                this.setData({
                    maxLength: 1000
                })
                wx.setNavigationBarTitle({
                    title: '发布推荐'
                })
            }
            this.setData({
                type: Number(options.type)
            })

            this.validatorInstance = new WeValidator({
                rules: {
                    description: {
                        required: true,
                    },
                },
                messages: {
                    description: {
                        required: '请输入内容',
                    },
                },
            })
        },
        textInput(e) {
            const cursor = e.detail.cursor
            this.setData({
                textLength: cursor > this.data.maxLength ? this.data.maxLength : cursor
            })
        },
        chooseImage(e) {
            const { file } = e.detail;
            wx.showLoading({
                title: '正在上传中',
                mask: true
            })
            Promise.allSettled(file.map(item => {
                return upload(item.url, false)
            })).then(url => {
                wx.hideLoading()
                this.setData({
                    fileList: [...this.data.fileList, ...url.map(item => {
                        return { url: item.value }
                    })]
                })
            })
        },
        removeImage(e) {
            const { index } = e.detail
            let data = this.data.fileList
            data.splice(index, 1)
            this.setData({
                fileList: data
            })
        },
        chooseVideo(e) {
            const { file } = e.detail;
            upload(file.url)
                .then(url => {
                    this.setData({
                        videoList: [{ url }]
                    })
                })
        },
        removeVideo(e) {
            const { index } = e.detail
            let data = this.data.videoList
            data.splice(index, 1)
            this.setData({
                videoList: data
            })
        },
        submit(e) {
            const value = e.detail.value

            if (!this.data.province) {
                wx.showToast({
                    title: '请选择地址',
                    icon: 'none'
                })
                return false
            }

            if (!this.data.fileList.length && !this.data.videoList.length) {
                wx.showToast({
                    title: '请上传图片/视频',
                    icon: 'none'
                })
                return false
            }

            let imgs = this.data.fileList.map(item => {
                return {
                    type: 1,
                    content: item.url
                }
            })
            imgs.push(...this.data.videoList.map(item => {
                return {
                    type: 2,
                    content: item.url
                }
            }))

            if (this.validatorInstance.checkData(value)) {
                this.request.run({
                    ...value,
                    imgs,
                    province: this.data.province,
                    city: this.data.city,
                    area: this.data.area,
                    address: this.data.address
                }).then(() => {
                    wx.showToast({
                        title: '发布成功'
                    })
                    wx.navigateBack()
                })
            }
        },
        changeLocation() {
            wx.getSetting({
                success: result => {
                    if (!result.authSetting.hasOwnProperty('scope.userLocation') || result.authSetting["scope.userLocation"]) {
                        wx.chooseLocation({
                            success: res => {
                                map.reverseGeocoder({
                                    location: {
                                        latitude: res.latitude,
                                        longitude: res.longitude
                                    },
                                    success: (res) => {
                                        this.setData({
                                            province: res.result.address_component.province,
                                            city: res.result.address_component.city,
                                            area: res.result.address_component.district,
                                            address: res.result.address
                                        })
                                    }
                                })
                            }
                        })
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '无法获取您的位置，点击确认以打开授权设置页面',
                            success: result1 => {
                                if (result1.confirm) {
                                    wx.openSetting()
                                }
                            }
                        })
                    }
                }
            })

        }
    }
});
